var divName = "theLayer";
var globalDiv;

function getXMLHTTP()
{
    //alert("in getXMLHTTP()");

    var xmlHttpRequest = null;
    try
    {
        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");

    }
    catch(e)
    {
        try
        {
            xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(oc)
        {
            xmlHttpRequest = null;
        }
    }

    if(!xmlHttpRequest && typeof XMLHttpRequest != "undefined")
    {
        xmlHttpRequest = new XMLHttpRequest();
    }

    return xmlHttpRequest;
}

function editUser(id, counter)
{
    var div;
    xmlHttp = getXMLHTTP();

    if(xmlHttp && xmlHttp.readyState != 0) {
        alert("not ready for xmlHTTP call");
    }

    if(xmlHttp) {

        xmlHttp.open("GET", "/Edit?method=editUser&id="+id+"&counter="+counter, true);

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4)
            {
                if(xmlHttp.status == 200)
                {
                    var responseString = xmlHttp.responseText;
                    if(responseString != "ERROR") {
                        nAgt = navigator.userAgent;
                        if((verOffset=nAgt.indexOf("Chrome"))!=-1)
                        {
                            div = createNewDiv_Center(500, 250);
                        }
                        else
                        {
                            div = createNewDiv_Center(500, 255);
                        }

                        while (div.childNodes.length > 0)
                            div.removeChild(div.childNodes[0]);
                        div.innerHTML = responseString;
                        showDiv(true);
                    }
                    else {
                        showErrorDiv();
                    }

                }
            }
        };

        xmlHttp.send(null);
    }
}

function getMidwidth() {
    var browserName=navigator.appName;
    if (browserName=="Microsoft Internet Explorer") {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    }
    else {
        winW = window.innerWidth;
        winH = window.innerHeight;
    }
}

function showErrorDiv(XMLString) {
    var div = createNewDiv_Center(250, 70)

    while (div.childNodes.length > 0) {
        div.removeChild(div.childNodes[0]);
    }

    myform = document.createElement("form");
    myform.setAttribute("name", "formSelectError")
    mytable = document.createElement("table");
    mytable.setAttribute("width", "100%");
    mytablebody = document.createElement("tbody");

    row_1 = document.createElement("tr");
    row_1.setAttribute("bgcolor", "#dae0e3");
    row_1.setAttribute("align", "left");
    row_1.setAttribute("colspan", "2");
    cell_11 = document.createElement("th");
    text_1 = document.createTextNode("Error Message");
    cell_11.appendChild(text_1);
    row_1.appendChild(cell_11);

    row_2 = document.createElement("tr");
    row_2.setAttribute("colspan", "2");
    cell_21 = document.createElement("td");

    text_1 = "";

    row_3 = document.createElement("tr");
    cell_31 = document.createElement("td");
    cell_31.setAttribute("colspan", "2");
    cell_31.setAttribute("style", "padding-left:85px;");
    var closeDiv = document.createElement("div");
    //closeDiv.setAttribute("align", "center");
    var closeTag = document.createElement("a");
    closeTag.setAttribute("href", "javascript:closeDiv()");
    closeTag.setAttribute("title", "Close");
    closeTag.innerHTML = "Close";
    closeDiv.appendChild(closeTag);
    cell_31.appendChild(closeDiv);
    row_3.appendChild(cell_31);

    if(XMLString == null || XMLString == "") {
        text_1 = document.createTextNode("An error occured while saving records.");
    }
    else {
        text_1 = document.createTextNode(XMLString);
    }

    cell_21.appendChild(text_1);
    row_2.appendChild(cell_21);

    mytablebody.appendChild(row_1);
    mytablebody.appendChild(row_2);
    mytablebody.appendChild(row_3);

    mytablebody.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif";
    mytablebody.style.fontSize = "10px";

    mytable.appendChild(mytablebody);
    myform.appendChild(mytable)
    div.appendChild(myform);

    showDiv(true);
}


function closeDiv() {
    showDiv(false);
}
function showDiv(show) {
    var div = document.getElementById(divName);

    if(show) {
        div.style.visibility = "visible";
    }
    else {
        div.style.visibility = "hidden";
    }
}
function createNewDiv_Center(width, height) {
    if (!globalDiv) {
        var newDiv = document.createElement("div");
        newDiv.setAttribute("id", divName);
        document.body.appendChild(newDiv);
    }

    globalDiv = document.getElementById(divName)

    while (globalDiv.childNodes.length > 0) {
        globalDiv.removeChild(globalDiv.childNodes[0]);
    }

    //globalDiv = document.getElementById(divName)

    globalDiv.style.background = "#dae0e3";
    globalDiv.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif";
    globalDiv.style.padding = "0px";
    globalDiv.style.border = "1px solid #000000";
    globalDiv.style.fontSize = "11px";
    globalDiv.style.position = "absolute";
    //globalDiv.style.filter = "alpha(opacity= 50); -moz-opacity:0.5; opacity: 0.5";

    getMidwidth();

    globalDiv.style.left = ((winW - width) / 2) + "px";
    globalDiv.style.top = 193 + "px";
    //globalDiv.style.width = 450 + "px";
    //globalDiv.style.height = 180 + "px";
    globalDiv.style.width = parseInt(width) + "px";
    globalDiv.style.height = parseInt(height) + "px";
    globalDiv.style.zIndex = 10000;
    //globalDiv.style.overflow = "auto";

    return globalDiv;
}
function updateUser(id)
{
    var fName = document.getElementById("fName");
    var lName = document.getElementById("lName");
    var email = document.getElementById("email");
    var loginID = document.getElementById("loginID");
    var password = document.getElementById("password");

    if(fName.value.length == 0)
    {
        alert("Please enter first name!");
        fName.focus();
        return false;
    }
    if(lName.value.length == 0)
    {
        alert("Please enter last name!");
        lName.focus();
        return false;
    }
    if(email.value.length == 0)
    {
        alert("Please enter email!");
        email.focus();
        return false;
    }
    if ((email.value).length > 0 && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value)))
    {
        alert("Invalid E-mail Address! Please re-enter.")
        email.focus();
        return false;

    }
    if(loginID.value.length == 0)
    {
        alert("Please enter logonid!");
        loginID.focus();
        return false;
    }
    if(password.value.length == 0)
    {
        alert("Please enter password!");
        password.focus();
        return false;
    }
    showDiv(false);
    document.getElementById("waitDiv").innerHTML="<img src='/images/please_wait.gif'>";
    var xmlHttp = getXMLHTTP();
    //alert('test1 point');
    if(xmlHttp && xmlHttp.readyState != 0)
    {
        alert("Not ready for xmlHTTP call");
    }
    if(xmlHttp)
    {
        xmlHttp.open("GET", "/Edit?method=save&id="+id+"&loginID="+loginID.value+"&fName="+fName.value+"&lName="+lName.value+"&email="+email.value+"&password="+password.value, true);

        xmlHttp.onreadystatechange = function()
        {
            if (xmlHttp.readyState == 4)
            {

                var actualText = xmlHttp.responseText;
                if(actualText == "save")
                {
                    document.getElementById("waitDiv").innerHTML=""
                    document.getElementById("waitDiv").innerHTML = "<p style='padding-top: 5px;color:Green'><span>Record updated successfully.</span></p>"
                    var counter = document.getElementById("counter").value;
                    document.getElementById("loginIDTD"+counter).innerHTML=loginID.value;
                    document.getElementById("fNameTD"+counter).innerHTML=fName.value;
                    document.getElementById("lNameTD"+counter).innerHTML=lName.value;
                    document.getElementById("emailTD"+counter).innerHTML=email.value;
                }
                else
                {
                    document.getElementById("waitDiv").innerHTML="<p style='padding-top: 5px;color:Red'><span>Unable to update record</span></p>"
                    
                }

            }
        };
        xmlHttp.send(null);
    }
}
function editCategory(id, counter)
{
    var div;
    xmlHttp = getXMLHTTP();

    if(xmlHttp && xmlHttp.readyState != 0) {
        alert("not ready for xmlHTTP call");
    }

    if(xmlHttp) {

        xmlHttp.open("GET", "/Edit?method=editCategory&id="+id+"&counter="+counter, true);

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4)
            {
                if(xmlHttp.status == 200)
                {
                    var responseString = xmlHttp.responseText;
                    if(responseString != "ERROR") {
                        nAgt = navigator.userAgent;
                        if((verOffset=nAgt.indexOf("Chrome"))!=-1)
                        {
                            div = createNewDiv_Center(500, 150);
                        }
                        else
                        {
                            div = createNewDiv_Center(500, 155);
                        }

                        while (div.childNodes.length > 0)
                            div.removeChild(div.childNodes[0]);
                        div.innerHTML = responseString;
                        showDiv(true);
                    }
                    else {
                        showErrorDiv();
                    }

                }
            }
        };

        xmlHttp.send(null);
    }
}


function updateCategory(id)
{
    var cName = document.getElementById("cName");


    if(cName.value.length == 0)
    {
        alert("Please enter category name!");
        cName.focus();
        return false;
    }

    showDiv(false);
    document.getElementById("waitDiv").innerHTML="<img src='/images/please_wait.gif'>";
    var xmlHttp = getXMLHTTP();
    //alert('test1 point');
    if(xmlHttp && xmlHttp.readyState != 0)
    {
        alert("Not ready for xmlHTTP call");
    }
    if(xmlHttp)
    {
        xmlHttp.open("GET", "/Edit?method=saveCategory&id="+id+"&cName="+cName.value, true);

        xmlHttp.onreadystatechange = function()
        {
            if (xmlHttp.readyState == 4)
            {

                var actualText = xmlHttp.responseText;
                if(actualText == "save")
                {
                    document.getElementById("waitDiv").innerHTML=""
                    document.getElementById("waitDiv").innerHTML = "<p style='padding-top: 5px;color:Green'><span>Record updated successfully.</span></p>"
                    var counter = document.getElementById("counter").value;
                    document.getElementById("cNameTD"+counter).innerHTML=cName.value;
                }
                else
                {
                    document.getElementById("waitDiv").innerHTML="<p style='padding-top: 5px;color:Red'><span>Unable to update record</span></p>"

                }

            }
        };
        xmlHttp.send(null);
    }
}